package marchsoft.modules.tiku.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import marchsoft.annotation.AnonymousAccess;
import marchsoft.annotation.rest.AnonymousDeleteMapping;
import marchsoft.annotation.rest.AnonymousGetMapping;
import marchsoft.annotation.rest.AnonymousPostMapping;
import marchsoft.annotation.rest.AnonymousPutMapping;
import marchsoft.base.PageVO;
import marchsoft.enums.ResultEnum;
import marchsoft.exception.BadRequestException;
import marchsoft.modules.system.entity.Menu;
import marchsoft.modules.system.entity.dto.MenuDTO;
import marchsoft.response.Result;
import marchsoft.utils.SecurityUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.EscapedErrors;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;

import marchsoft.modules.tiku.service.IESlideshowService;
import marchsoft.modules.tiku.entity.ESlideshow;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


/**
* <p>
* 轮播图 前端控制器
* </p>
* @author zsy
* @since 2022-03-29
*/
@RequiredArgsConstructor
@RestController
@Slf4j
@Api(tags = "轮播图模块")
@RequestMapping("/api/eSlideshow")
public class ESlideshowController {
    private final IESlideshowService eSlideshowService;

//    @ApiOperation("获取全部的轮播图")
//    @AnonymousGetMapping
//    public Result<Object> getAllSlide(){
//        log.info("【获得所有轮播图 /api/eSlideshow】",SecurityUtils.getCurrentUserId());
//        List<ESlideshow> allSide=eSlideshowService.getAllSlide();
//        return Result.success(allSide);
//    }

    @ApiOperation("导出用户数据")
    @GetMapping(value = "/all/download")
    public void download(HttpServletResponse response, ESlideshow eSlideshow) throws IOException {
        //每页条数设置为-1，是查询全部
        PageVO pageVO = new PageVO();
        pageVO.setSize(-1);
        Result<IPage<ESlideshow>> pageResult = getAll(eSlideshow, pageVO);
        List<ESlideshow> eSlideshowList= pageResult.getData().getRecords();
        eSlideshowService.download(eSlideshowList, response);
    }
    @ApiOperation("根据条件获取轮播图信息")
    @AnonymousGetMapping("/all")
    public Result<IPage<ESlideshow>> getAll(ESlideshow eSlideshow,PageVO pageVO){
        log.info("【根据条件获取轮播图信息 api/eSlideshow/all】操作人：",SecurityUtils.getCurrentUserId(),pageVO);
        IPage<ESlideshow> all=eSlideshowService.getAll(pageVO.buildPage());
        return Result.success(all);
    }
    @ApiOperation("修改轮播图")
    @AnonymousPutMapping
    public Result<Object> update(@RequestBody ESlideshow eSlideshow){
        log.info("【修改轮播图 /api/eSlideshow】操作人id: ", SecurityUtils.getCurrentUserId(),eSlideshow.getId());
        eSlideshowService.updateSlide(eSlideshow);
        return Result.success();
    }
    @ApiOperation("删除轮播图")
    @AnonymousDeleteMapping
    public Result<Void> delete( @RequestBody Set<Long> ids){
        if(CollectionUtil.isEmpty(ids)){
            throw new BadRequestException(ResultEnum. PARAM_IS_BLANK);
        }
        log.info("【删除轮播图 /api/eSlideshow】操作人员：",SecurityUtils.getCurrentUserId(),ids);
        eSlideshowService.deleteByIds(ids);
//        eSlideshowService.removeByIds(ids);
        return Result.success();
    }
    @ApiOperation("添加轮播图")
    @AnonymousPostMapping
    //使用@PostMapping 需要权限
    public Result<Object> add(@RequestBody ESlideshow eSlideshow){
        log.info("【添加轮播图 /api/eSlideshow】操作人员：",SecurityUtils.getCurrentUserId(),eSlideshow);
        eSlideshowService.addSlide(eSlideshow);
        return Result.success();
    }

}
